package com.wnxy.sdd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.sdd.entity.Subject;
import com.wnxy.sdd.mapper.SubjectMapper;
import com.wnxy.sdd.model.vo.SubjectTreeVo;
import com.wnxy.sdd.model.vo.SubjectVo;
import com.wnxy.sdd.service.ISubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.sdd.util.ExcelUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2023-06-16
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {

    /**
     * @SneakyThrows
     * 1、抑制检查异常，会把检查异常转换为运行时期异常
     * 2、如果不加这个注解file.getInputStream()会有一个检查异常，必须要写try..catch;
     * 3、写上这个注解后就不用在处理检查异常了。
     */
    @SneakyThrows
    @Override
    public void importExcel(MultipartFile file) {
        // 导入excel获取数据
        List<SubjectVo> subjectVos =
                ExcelUtils.importExcel(file.getInputStream(), SubjectVo.class);
        // 判断，遍历数据
        if (subjectVos != null && subjectVos.size() > 0) {

            subjectVos.stream().forEach(subjectVo -> {
                // 根据title查询课程类目（一级类目）,如果不存在则插入
                Subject subjectOne = findByTitle(subjectVo.getName());
                if (subjectOne == null) {
                    subjectOne = new Subject();
                    // 一级分类为空，插入一级分类
                    subjectOne.setTitle(subjectVo.getName());
                    subjectOne.setParentId("0");
                    this.save(subjectOne);
                }

                // 根据课程名称查询
                Subject subjectTwo = findByTitle(subjectVo.getParentName());
                if (subjectTwo == null) {
                    subjectTwo = new Subject();
                    // 二级分类为空，插入二级分类
                    subjectTwo.setTitle(subjectVo.getParentName());
                    subjectTwo.setParentId(subjectOne.getId());
                    this.save(subjectTwo);
                }
            });
        }
    }

    /**
     * 根据title查询课程表
     */
    private Subject findByTitle(String title) {
        return this.getOne(
                Wrappers.lambdaQuery(Subject.class).eq(Subject::getTitle,title));
    }

    @Override
    public List<SubjectTreeVo> findSubjects() {
        // 1. 查询一级分类： parent_id=0 是一级分类
        List<Subject> subjectOneList = findSubjectByParentId("0");

        // 2. 遍历一级分类； 根据一级分类id查询children
        List<SubjectTreeVo> result = subjectOneList.stream().map(subject -> {
            // 创建SubjectTreeVo对象: 封装一级分类
            SubjectTreeVo subjectTreeVo = new SubjectTreeVo();
            subjectTreeVo.setId(subject.getId());
            subjectTreeVo.setTitle(subject.getTitle());

            // 封装二级分类: select * from sdd_subject where parent_id='257'/'426'
            List<Subject> childrenSubjectList = this.findSubjectByParentId(subject.getId());
            List<SubjectTreeVo> children = childrenSubjectList.stream().map(childrenSubject -> {
                SubjectTreeVo childrenVo = new SubjectTreeVo();
                childrenVo.setId(childrenSubject.getId());
                childrenVo.setTitle(childrenSubject.getTitle());
                return childrenVo;
            }).collect(Collectors.toList());
            // 设置二级分类
            subjectTreeVo.setChildren(children);

            return subjectTreeVo;
        }).collect(Collectors.toList());
        return result;
    }


    /**
     * 根据parent_id查询
     */
    private List<Subject> findSubjectByParentId(String parentId) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<Subject> subjectList = this.list(wrapper);
        return subjectList;
    }
}
